package com.gzhryc.shared_device.oem.h5.mng.controllers.member;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.member.dao.db.Member;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberAddress;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberEnum;
import com.gzhryc.shared_device.oem.code.member.services.MemberAddressService;
import com.gzhryc.shared_device.oem.code.member.services.MemberService;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberSearch;
import com.gzhryc.shared_device.oem.h5.mng.MemberTableHelper;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class MemberController {

    static Logger log = Logger.getLogger(MemberController.class);

    @Route(value = { "/member/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("typeList", MemberEnum.Type.values());
        request.setAttribute("idPrefixList", MemberTableHelper.getTableItem());
        MemberTableHelper.refresh();
        return "/member/list.html";
    }

    @Route(value = "/member/list.data")
    public ListResult<Member> listData(HttpServletRequest request) {
        ListResult<Member> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        MemberSearch search = WebUtils.getParameter(request,MemberSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        if(search.getIdPrefix() == null && search.getId() != null){
            search.setIdPrefix(search.getId());
        }

        result.getBody().setTotal(MemberService.self().countBySearch(search));
        List<Member> memberList = MemberService.self().findBySearch(search,pageNo,pageLimit);
        if(memberList != null && memberList.size() > 0){
            String resDomain = PropertyTools.get().getString("resDomain");
            String memberFaceImg = PropertyTools.get().getString("memberFaceImg");
            for(Member member : memberList){
                if(StringTools.isNotBlank(member.getFaceImg())){
                    member.setFaceImg(WebUtils.setHttpDomain(resDomain,member.getFaceImg()));
                }else {
                	member.setFaceImg(memberFaceImg);
                }
            }
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(memberList);
        }
        return result;
    }

    @Route(value = { "/member/info.html" })
    public String info(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        Member member = MemberService.self().getById(id);
        if(member != null) {
            String resDomain = PropertyTools.get().getString("resDomain");
            String memberFaceImg = PropertyTools.get().getString("memberFaceImg");
            if(StringTools.isNotBlank(member.getFaceImg())){
                member.setFaceImg(WebUtils.setHttpDomain(resDomain,member.getFaceImg()));
            }else {
            	member.setFaceImg(memberFaceImg);
            }
            WebUtils.setAttribute(request, member);
        }
        return "/member/info.html";
    }

    @Route(value = "/member/address/list.data")
    public ListResult<MemberAddress> addressListData(HttpServletRequest request) {
        ListResult<MemberAddress> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Long memberId = WebUtils.getLong(request,"memberId");
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(MemberAddressService.self().countByMemberId(memberId));
        List<MemberAddress> memberAddressList = MemberAddressService.self().findByMemberId(memberId,pageNo,pageLimit);
        if(memberAddressList != null && memberAddressList.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(memberAddressList);
        }
        return result;
    }

    @Route(value = { "/member/address/del.do" })
    public AjaxResult delAddress(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        Long memberId = WebUtils.getLong(request, "memberId");
        Long addressId = WebUtils.getLong(request,"addressId");
        try {
            if(MemberAddressService.self().deleteByKeys(memberId,addressId)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/member/del.do" })
    public AjaxResult del(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        Long id = WebUtils.getLong(request, "id");
        try {
            if(MemberService.self().deleteById(id)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
